package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseArrayDialogFragment;
import com.isunland.managesystem.base.BaseSelectObject;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.OderSaleContent;
import com.isunland.managesystem.entity.OrderQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/28 16:35
 */

public class OrderQueryFragment extends BaseQueryFragment {
  @BindView(R.id.slv_startTime) SingleLineView slvStartTime;
  @BindView(R.id.slv_endTime) SingleLineView slvEndTime;
  @BindView(R.id.slv_outState) SingleLineView slvOutState;
  @BindView(R.id.slv_customerName) SingleLineView slvCustomerName;
  private OrderQueryParams mParams;

  @Override public void initData() {
    super.initData();
    mParams = (!(mBaseParams instanceof OrderQueryParams)) ? new OrderQueryParams()
        : (OrderQueryParams) mBaseParams;
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_order_query, container, false);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    slvStartTime.getTvContent().setText(mParams.getBeginregDate());
    slvEndTime.getTvContent().setText(mParams.getEndregDate());
    slvOutState.getTvContent().setText(mParams.getDataStatusName());
    slvCustomerName.getTvContent().setText(mParams.getCustomerName());

    slvStartTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(
            BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(mParams.getBeginregDate()))
                .setCallBack(new BaseYMDTimeDialogFragment.CallBack() {
                  @Override public void select(Date date) {
                    mParams.setBeginregDate(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                    slvStartTime.getTvContent()
                        .setText(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                  }
                }));
      }
    });
    slvEndTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(mParams.getEndregDate()))
            .setCallBack(new BaseYMDTimeDialogFragment.CallBack() {
              @Override public void select(Date date) {
                mParams.setEndregDate(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                slvEndTime.getTvContent().setText(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
              }
            }));
      }
    });
    slvOutState.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(SimpleArrayDialogFragment.newInstance(OderSaleContent.getAllOutList(mActivity))
            .setCallBack(new BaseArrayDialogFragment.CallBack<BaseSelectObject>() {
              @Override public void select(BaseSelectObject base) {
                mParams.setDataStatusName(base.getName());
                mParams.setDataStatus(base.getCode());
                mParams.setDataStatusId(base.getId());
                slvOutState.getTvContent().setText(base.getName());
              }
            }));
      }
    });
  }

  @Override protected BaseParams confirm() {
    Date startDate = MyDateUtil.parse(mParams.getBeginregDate());
    Date endDate = MyDateUtil.parse(mParams.getEndregDate());
    //不再同一天时,结束时间不能早于开始时间
    if ((startDate != null)
        && (endDate != null)
        && (!MyDateUtil.isSameDay(startDate, endDate))
        && endDate.before(startDate)) {
      ToastUtil.show(R.string.stopDateBeforeStartDate);
      return null;
    }
    mParams.setCustomerName(slvCustomerName.getTextContent());
    return mParams;
  }

  @Override protected void clear() {
    //更新数据
    mParams.setBeginregDate("");
    mParams.setEndregDate("");
    mParams.setCustomerName("");
    mParams.setDataStatus("");
    mParams.setDataStatusName("全部");

    //更新页面
    slvStartTime.clear();
    slvEndTime.clear();
    slvCustomerName.clear();
    slvOutState.getTvContent().setText("全部");
  }
}
